#include "pch.h"
#include "SegmentWithC.h"


int SegmentWithC::findPreviousPoint(int idx, float* pfOUT) {
	for (int i = idx - 1; i >= 0; i--) {
		if (pfOUT[i] > 0)
			return i;
	}
	return 0;
}

void SegmentWithC::setSegment(int previousIdx, float previousPrice, int idx, float price, float* pfOUT) {
	float dv = (price - previousPrice) / (idx - previousIdx);
	for (int i = previousIdx + 1; i < idx; i++) {
		pfOUT[i] = pfOUT[i - 1] + dv;
	}
	pfOUT[idx] = price;
}

void SegmentWithC::drawSegment(int dataLen, float* pfOUT, vector<KLine*>* kLines) {
	pfOUT[0] = kLines->at(0)->m_close;
	for (int i = 1; i < dataLen; i++) {
		if (kLines->at(i)->isPoint()) {
			int lastPoint = findPreviousPoint(i, pfOUT);
			float lastPrice = pfOUT[lastPoint];
			setSegment(lastPoint, lastPrice, i, kLines->at(i)->m_close, pfOUT);
		}
	}
}